var ajaxStarted = false;
$(document).ready(function(){
  
  $("#checkAll").change({"class":".checkDel"},checkAllCheckboxChanged);  
  $('.checkDel').change({"check":"#checkAll","class":".checkDel"},classCheckboxChanged);
  
  $("#filterAllNotice").change({"class":".noteFilter"},checkAllCheckboxChanged);  
  $('.noteFilter').change({"check":"#filterAllNotice","class":".noteFilter"},classCheckboxChanged);
  
  $("#filterAllDevice").change({"class":".deviceFilter"},checkAllCheckboxChanged);  
  $('.deviceFilter').change({"check":"#filterAllDevice","class":".deviceFilter"},classCheckboxChanged);
  
  $('#devSelector').change(filterChanged);
  $('#filterAllNotice').change(filterChanged);
  $('#filterAllDevice').change(filterChanged);
  $('.deviceFilter').change(filterChanged);
  $('.noteFilter').change(filterChanged);
  
  $('#deleteBtn').click(btnClicked);
});

function filterChanged(){
  var devicesList = Array();
  var noteList = Array();
  var hiddenTrInRow = 0;
  $('.deviceFilter').each(function(){
    if($(this).is(':checked'))
    devicesList.push($(this).val().trim());
  });
  $('.noteFilter').each(function(){
    if($(this).is(':checked'))
    noteList.push($(this).val().trim());
  });
  $('#notificationsList tbody tr.padding').remove();
  $('#notificationsList tbody tr').show().each(function(){
    if($.inArray($(this).find('td:nth-last-of-type(2)').text().trim(),devicesList) == -1 
    || $.inArray($(this).find('td:nth-last-of-type(3)').text().trim(),noteList) == -1
    || ($('#devSelector').val() !== '0' && $('#devSelector').val().trim() !== $(this).find('td:nth-last-of-type(1)').text().trim()))
    {
      $(this).hide();
      hiddenTrInRow++;
    }
    else
    {
      if((hiddenTrInRow%2)>0)
        $(this).before($('<tr>').attr('class','padding').hide());
      hiddenTrInRow=0;
    }
  });
  $('.checkDel').trigger('change');
}

function classCheckboxChanged(e){
  var allChecked= true;
    $(e.data.class+":visible").each(function(){
     if(!$(this).is(':checked'))
       allChecked = false;
    });
    if(allChecked)
    {
      $(e.data.check).prop('checked',true);
    }
    else
      $(e.data.check).prop('checked',false);
}

function checkAllCheckboxChanged(e){
  if($(this).is(':checked'))
    {
        $(e.data.class+":visible").prop('checked', true);
    }else
    {
        $(e.data.class+":visible").prop('checked', false);
    }
}

function btnClicked()
{
  if(!ajaxStarted)
  {
    ajaxStarted = true;
    $('#picArea').empty().append($('<img>')
              .attr({"src":"../design/pictures/loading.gif",
                     "width":"16px",
                     "alt":"loading"}));
                   
    $.post("ajax/deleteFlags.php",$('#flagForm').serialize()).done(function(data){
      var jsonData =$.parseJSON(data);
      $('#notificationsList tbody').empty().append(jsonData.flags);
      $('#filterTable tbody').empty().append(jsonData.filter);
      $('#picArea').empty();
      ajaxStarted = false;
    }).fail(function(){
      $('#picArea').empty().append($('<img>')
              .attr({"src":"../design/pictures/error.png",
                     "width":"16px",
                     "alt":"error"}));
                   
      ajaxStarted = false;
    });
  }
}